package cn.tedu._03vehicle.controller;


import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@Tag(name = "01-用户管理模块")
@RequestMapping("/v1/user")
@Slf4j
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @Operation(summary = "用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("传入的参数："+userRegParam);
        String username = userRegParam.getUsername();
        log.debug("用户名："+username);
        int count = userMapper.selectByUsername(username);
        log.debug("用户名查询结果："+count);
        if(count>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS,"用户名被占用！");
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        log.debug("转换后的参数："+user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("插入数据库的用户信息："+user);
        int row=userMapper.insertUser(user);
        if(row>0){
            return JsonResult.ok("用户注册成功！");
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }


    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {@Parameter(name = "username",description = "用户名",required = true,example = "admin"),
    @Parameter(name="password",description = "密码",required = true,example = "123456"),
            @Parameter(name ="userLoginParam",hidden = true)
    })
    public JsonResult login(UserLoginParam userLoginParam, HttpSession session){
        log.debug("用户名："+userLoginParam.getUsername());
        UserVO userVO = userMapper.selectUserByUsernameAndPassword(userLoginParam);
        if(userVO==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok("username:"+userLoginParam.getUsername()+"   password:"+userLoginParam.getPassword());
    }

    @PostMapping("currentUser")
    @Operation(summary = "获取当前登录用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }

    @GetMapping("logout")
    @Operation(summary = "退出登录")
    public void logout(HttpSession session){
        //删除session会话中的用户标识
        session.removeAttribute("user");
    }
}
